/* check the first radio button in group */
$('.wid-cus  input[type="radio"]').each(function(){
	name = $(this).attr('name');
	$('.wid-cus  input[name="'+name+'"]').first().trigger('click');
});


/* widget content replace by form values */
function replace_1000(src, rekey, bykey) {
	if (src != null) {
		for (var i = 0; i < 1000; i++) {
			index = src.indexOf(rekey);
			if (index == -1) {
				break;
			}
			src = src.replace(rekey, bykey);
		}
	}
	return src;
}

var widget_content = $('.widget-content').val();
var widget_result = '';
function widcus_replace() {	
	widget_result = widget_content;

	$('.wid-cus  input[type="checkbox"]').each(function(){
		checked = $(this).is(':checked');
		if (checked) {
			val = $(this).attr('true');
		} else {
			val = $(this).attr('false');
		}
		name = '$'+$(this).attr('name')+'$';
		widget_result = replace_1000(widget_result, name, val);
	});
	
	$('.wid-cus  input[type="radio"]').each(function(){				
		name = $(this).attr('name');		
		val = $('.wid-cus  input[name="'+name+'"]:checked').val();
		name = '$'+name+'$';	
		widget_result = replace_1000(widget_result, name, val);
	});	
	
	$('.wid-cus  input[type="text"]').each(function(){				
		val = $(this).val();
		name = '$'+$(this).attr('name')+'$';
		widget_result = replace_1000(widget_result, name, val);
	});	
	
	$('.wid-cus  select').each(function(){				
		name = '$' + $(this).attr('name') + '$';		
		val = $(this).val();
		widget_result = replace_1000(widget_result, name, val);
	});	
	
	$('.wid-cus  textarea').each(function(){				
		name = '$' + $(this).attr('name') + '$';		
		val = $(this).val();
		widget_result = replace_1000(widget_result, name, val);
	});	
	
	$('.widget-content').val(widget_result);
}


/*widget form validator*/
function val_min(val, min) {
	if (min != null && !isNaN(min)) {
		if (val == null) {
			return false;
		}
		min = Number(min);
		if (min > 0 && val.length < min) {
			return false;
		}
	} 
	return true;
}

function val_max(val, max) {
	if (max != null && !isNaN(max)) {
		if (val != null) {
			max = Number(max);
			if (max > 0 && val.length > max && max < 10000) {
				return false;
			}
		}
	} 
	return true;
}

function val_req(val, req) {
	if (req == 'true') {
		if (val == null || val == '') {
			return false;
		} 
	}
	return true;
}

function val_url(val, url) {
	if (url == 'true') {
		if (val == null) {
			return false;
		}
		if (val.indexOf('http://') == -1 && val.indexOf('https://') == -1) {
			return false;
		}
		key = '://';index = val.indexOf(key);
		val = val.substring(index+key.length);
		key = 'www.';index = val.indexOf(key);
		if (index != -1) {
			val = val.substring(index+key.length);
		}
		key = '.';index = val.indexOf(key);
		if (index == -1) {
			return false;
		}
		pre = val.substring(0,index);
		pos = val.substring(index+key.length);
		
		if (pre.length < 3 || pos.length < 2) {
			return false;
		}
	}
	return true;
}

function val_image(val, img) {
	if (img == 'true') {
		if (!val_url(val, 'true')) {
			return false;
		}
		
		key = '://';index = val.indexOf(key);
		val = val.substring(index+key.length);
		
		key = '/';index = val.indexOf(key);
		if (index == -1) {
			return false;
		}
		val = val.substring(index+key.length);
		
		
		val = val.toLowerCase();
		key = '.jpg';
		pre = val.substring(0, val.length - key.length);
		ext = val.substring(val.length - key.length);
		if (ext != '.jpg' && ext != '.png' && ext != '.gif' && ext != '.bmp' || pre.length < 1) {
			return false;
		}
	}
	return true;
}

function val_number(val, num) {
	if (num == 'true') {
		if (val == null) {
			return false;
		}
		if (isNaN(val)) {
			return false;
		}
	}
	return true;
}


function val_alert(name, msg){	
	$('html, body').animate({
	scrollTop: $('*[name="'+name+'"]').offset().top - 50
	}, 500);
	$('<span class="val_alert val_alert'+name+'">'+msg+'</span>').insertAfter($('*[name="'+name+'"]'));
	$('span.val_alert'+name).show().fadeOut(5000);
	$('*[name="'+name+'"]').focus();
}


/*submit widget*/
var alt_wid = $('.wid-alt').html();

$('#wid-cus').submit(function(event){
	$('.val_alert').remove();
	var ret = true;
	$('.wid-cus  input[type="text"]').each(function(){
		if (ret == false) {
			return;
		}
		min = $(this).attr('min');
		max = $(this).attr('max');
		req = $(this).attr('require');
		url = $(this).attr('url');
		img = $(this).attr('image');
		num = $(this).attr('number');
		val = $(this).val();
		name = $(this).attr('name');
		
		if (!val_min(val,min)) {
			val_alert(name, 'Need more ' + min + ' characters');
			ret = false;
			return;
		}
		
		if (!val_max(val,max)) {
			val_alert(name, 'Need lower ' + max + ' characters');
			ret = false;
			return;
		}
		
		if (!val_req(val, req)) {
			val_alert(name, 'This field is require');
			ret = false;
			return;
		}
		
		if (!val_url(val,url)) {
			val_alert(name, 'Please input a right URL');
			ret = false;
			return;
		}
		
		if (!val_image(val,img)) {
			val_alert(name, 'Please input a right image URL');
			ret = false;
			return;
		}
		
		if (!val_number(val, num)) {
			val_alert(name, 'Please input a right number');
			ret = false;
			return;
		}
	});
	
	$('.wid-cus  textarea').each(function(){
		if (ret == false) {
			return;
		}
		min = $(this).attr('min');
		max = $(this).attr('max');
		req = $(this).attr('require');
		val = $(this).val();
		name = $(this).attr('name');
		
		if (!val_min(val,min)) {
			val_alert(name, 'Need more ' + min + ' characters');
			ret = false;
			return;
		}
		
		if (!val_max(val,max)) {
			val_alert(name, 'Need lower ' + max + ' characters');
			ret = false;
			return;
		}
		
		if (!val_req(val, req)) {
			val_alert(name, 'This field is require');
			ret = false;
			return;
		}
	});
	
	if (ret == true) {
		widcus_replace();
	}

	if (alt_wid != null && ret == true) {
		alt_wid_get();
		return false;
	}
	return ret;
})

function alt_wid_get() {
	$('.wid-alt .widget-alt-content').val($('.widget-content').val());

	$('.wid-alt').css('display', 'block');
	$('html, body').animate({
		scrollTop: $('.wid-alt .widget-alt-content').offset().top}, 300);
	$('.wid-alt .widget-alt-content').focus();
	$('.wid-alt .widget-alt-content').select();
}

